package com.googlecode.cswish.util;

import java.util.ArrayList;

public class EventArrayList<E> extends ArrayList<E> {

	public final static int ADD = 0;
	public final static int REMOVE = 1;
	public final static int SET = 2;
	
	private Event<E> event;

	public void registerEvent(Event<E> event) {
		this.event = event;
	}

	@Override
	public boolean add(E t) {
		if (event != null) {
			event.notice(ADD, t);
		}
		return super.add(t);
	}
	
	@Override
	public void add(int index, E element) {
		if (event != null) {
			event.notice(ADD, element);
		}
		super.add(index, element);
	}
	
	@Override
	public E set(int index, E element) {
		if (event != null) {
			event.notice(SET, element);
		}
		return super.set(index, element);
	}
	
	@Override
	public E remove(int index) {
		E element = super.remove(index);
		if (event != null) {
			event.notice(REMOVE, element);
		}
		return element;
	}
	
	@Override
	public boolean remove(Object o) {
		if (event != null) {
			event.notice(REMOVE, (E)o);
		}
		return super.remove(o);
	}
	
	public interface Event<E> {
		void notice(int operation, E object);
	}
}
